package com.warrantyhotel.android.widget;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.warrantyhotel.android.R;
import com.warrantyhotel.android.util.OnRedirectToListStores;
import com.warrantyhotel.android.util.StorePinch;
import com.warrantyhotel.android.util.imLoader1.ImageLoaderNew1;
import com.warrantyhotel.android.util.imLoader2.Utils;

public class StorePinMapOverlay extends ItemizedOverlay<StoreItem> {
	private ArrayList<StoreItem> mOverlays = new ArrayList<StoreItem>();
	private Context context;
	private MapView mapView;
	private String retailStore;
	private ImageLoaderNew1 im;
	public static int count = 0;
	private OnRedirectToListStores onRedirectToListStores;

	public StorePinMapOverlay(Drawable defaultMarker, Context context, MapView mapView, String retailStoreId, OnRedirectToListStores onRedirectToListStores) {
		super(defaultMarker);
		// super(boundCenterBottom(defaultMarker));
		this.context = context;
		this.mapView = mapView;
		this.retailStore = retailStoreId;
		im = new ImageLoaderNew1(context);
		this.onRedirectToListStores = onRedirectToListStores;

	}

	public void addOverlay(StoreItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	public String getRetailStoreId() {
		return retailStore;
	}

	@Override
	protected StoreItem createItem(final int i) {
		String url = mOverlays.get(i).getThumbnail();
		im.displayImage(url, (Activity) context, new ImageLoaderNew1.OnFetchCompleteListener1() {
			@Override
			public void onFetchComplete1(Bitmap result) {
				if (result != null) {

					Bitmap pinBg = BitmapFactory.decodeResource(context.getResources(), R.drawable.map_marker_logo);
					Bitmap bitmapWithReflection = Bitmap.createBitmap(pinBg.getWidth(), pinBg.getHeight(), Config.ARGB_8888);
					Canvas canvas = new Canvas(bitmapWithReflection);

					float sx = pinBg.getWidth() / result.getWidth();

					float sy = (float) pinBg.getHeight() / result.getHeight();

					float resize = (sx < sy) ? sy : sx;
					resize *= 0.6;

					float radius = 5.0f;

					DisplayMetrics metrics = new DisplayMetrics();
					((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

					switch (metrics.densityDpi) {
					case DisplayMetrics.DENSITY_MEDIUM:
						radius = 5.0f;
						break;
					case DisplayMetrics.DENSITY_HIGH:
						radius = 10.0f;
						break;
					}

					Matrix matrix = new Matrix();
					matrix.postScale(resize, resize);
					result = Utils.getRoundedCornerImage(Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, false));
					canvas.drawBitmap(pinBg, 0f, 0f, null);
					canvas.drawBitmap(result, radius, radius, null);

					Drawable b = new BitmapDrawable(bitmapWithReflection);
					b.setBounds(0 - b.getIntrinsicWidth() / 2, 0 - b.getIntrinsicHeight(), b.getIntrinsicWidth() / 2, 0);

					StoreItem item = mOverlays.get(i);
					Drawable icon = new BitmapDrawable(result);
					//
					pinBg.recycle();
					result.recycle();
					//
					icon.setBounds(0 - icon.getIntrinsicWidth() / 2, 0 - icon.getIntrinsicHeight(), icon.getIntrinsicWidth() / 2, 0);
					item.setMarker(b);
					mapView.invalidate();
				}
			}
		});
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	@Override
	protected boolean onTap(int index) {
		count++;
		StorePinch.getInstance(index, mOverlays, mapView, context, this.onRedirectToListStores, mOverlays.get(index).getPoint());
		return super.onTap(index);
	}

	// @Override
	// public boolean onTouchEvent(MotionEvent event, MapView mapView) {
	// mapView.removeAllViews();
	// return super.onTouchEvent(event, mapView);
	// }

	// class UpdateTimeTask extends TimerTask {
	// private int index;
	//
	// public UpdateTimeTask(int index) {
	// this.index = index;
	// }
	//
	// public void run() {
	// String id = mOverlays.get(index).getId();
	// String banner = mOverlays.get(index).getBanner();
	// if (id != null) {
	// mapView.getController().animateTo(mOverlays.get(index).getPoint());
	// final Intent intent = new Intent(context, NearbyStoresDetail.class);
	// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	// intent.putExtra(Keys._retailStoreId, id);
	// intent.putExtra(Keys.banner, banner);
	// context.startActivity(intent);
	// } else {
	// ((Activity) context).finish();
	// }
	// }
	// }
	//
	// @Override
	// public int hashCode() {
	// final int prime = 31;
	// int result = 1;
	// result = prime * result + ((retailStore == null) ? 0 :
	// retailStore.hashCode());
	// return result;
	// }
	//
	// @Override
	// public boolean equals(Object obj) {
	// if (this == obj)
	// return true;
	// if (obj == null)
	// return false;
	// if (getClass() != obj.getClass())
	// return false;
	// StorePinMapOverlay other = (StorePinMapOverlay) obj;
	// if (retailStore == null) {
	// if (other.retailStore != null)
	// return false;
	// } else if (!retailStore.equals(other.retailStore))
	// return false;
	// return true;
	// }

}
